﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Administracao_Cadastros_acGrupoEmail : System.Web.UI.Page
{
    private const int ItemsPerRequest = 10;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CarregaComboStatus(rcbStatus);
            CarregaTipoOrgan(rcbTipoOrgan);
            OcultaCampos(true);
        }
    }

    #region Eventos
    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
        Pesquisa();
        RadGrid1.DataBind();
        OcultaCampos(false);
    }
    protected void btnIncluir_Click(object sender, EventArgs e)
    {
        Server.Transfer("acCadGrupoEmail.aspx");
    }
    protected void btnLimpar_Click(object sender, EventArgs e)
    {

    }
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        Pesquisa();
    }
    protected void rcbDescricao_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        GrupoEmail grupoEmail = new GrupoEmail();
        List<GrupoEmail> listGrupo = grupoEmail.SelectPor(e.Text);

        int itemOffset = e.NumberOfItems;
        int endOffset = Math.Min(itemOffset + ItemsPerRequest, listGrupo.Count);
        e.EndOfItems = endOffset == listGrupo.Count;

        for (int i = itemOffset; i < endOffset; i++)
        {
            rcbDescricao.Items.Add(new RadComboBoxItem(listGrupo[i].Descricao, listGrupo[i].IdGrupoEmail.ToString()));
        }

        e.Message = GetStatusMessage(endOffset, listGrupo.Count);
    }
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {

            int idGrupoEmail = Int32.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["IdGrupoEmail"].ToString());

            Server.Transfer("acCadGrupoEmail.aspx?id=" + idGrupoEmail.ToString());
        }
    }
    #endregion

    #region Metodos
    protected List<GrupoEmail> CarregaGrupoEmail(string nome, RadComboBox comboStatus, RadComboBox comboTipo)
    {
        GrupoEmail grupoEmail = new GrupoEmail();

        List<Status> listStatus = new List<Status>();
        foreach (RadComboBoxItem item in comboStatus.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chk1");
            if (chk.Checked)
                listStatus.Add(new Status(int.Parse(item.Value)));
        }

        int idTipo = 0;
        if (comboTipo.SelectedValue != "")
            idTipo = int.Parse(comboTipo.SelectedValue);

        List<GrupoEmail> listGrupoEmail = grupoEmail.SelectPor(nome, listStatus, idTipo);

        return listGrupoEmail;
    }
    private void Pesquisa()
    {
        RadGrid1.DataSource = CarregaGrupoEmail(rcbDescricao.Text, rcbStatus, rcbTipoOrgan);
    }
    protected void CarregaComboStatus(RadComboBox combo)
    {
        Status status = new Status();
        List<Status> listStatus = status.SelectAll();

        combo.DataTextField = "Descricao";
        combo.DataValueField = "IdStatus";
        combo.DataSource = listStatus;
        combo.DataBind();
    }
    protected void CarregaTipoOrgan(RadComboBox combo)
    {
        TipoOrganizacao tipoOrganizacao = new TipoOrganizacao();
        List<TipoOrganizacao> listTipo = tipoOrganizacao.SelectGrupoEmail();

        combo.DataTextField = "Descricao";
        combo.DataValueField = "IdTipoOrganizacao";
        combo.DataSource = listTipo;
        combo.DataBind();
    }
    protected static string GetStatusMessage(int offset, int total)
    {
        if (total <= 0)
            return "No matches";

        return String.Format("Items <b>1</b>-<b>{0}</b> de <b>{1}</b>", offset, total);
    }
    protected void OcultaCampos(bool oculta)
    {
        trResultado.Visible = !oculta;
    }
    #endregion

}
